package gui.views;

import core.Model;
import core.utils.LogData;
import core.utils.Variable;
import core.utils.VariableDataSet;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.util.List;
import java.util.TreeSet;

public class GraphView extends JPanel {
	private static final long serialVersionUID = 1L;
	
    private final ChartPanel chartPanel = new ChartPanel(null);

    public GraphView() {
    	add(chartPanel);
        update();
    }
    
	public void update() {
		XYSeriesCollection dataSet = new XYSeriesCollection();
		TreeSet<VariableDataSet> data = Model.getInstance().getData();
		List<Variable> checked = Model.getInstance().getCheckedVariables();
		
		for (VariableDataSet v : data){
			if (checked.contains(v.getVariable())){
				XYSeries xy = new XYSeries(v.getVariable().getName());
				for (LogData dataPoint : v.getData()){
					xy.add(dataPoint.getTimeInMillis()/1000, dataPoint.getSignalValue());
				}
				dataSet.addSeries(xy);
			}
		}


        JFreeChart chart = ChartFactory.createXYLineChart("", "Time", "Signal Value", dataSet,
                PlotOrientation.VERTICAL, true, false, false);
        chartPanel.setChart(chart);
        revalidate();
	}
}
